#ifndef __DB_ACCESS_H__
#define __DB_ACCESS_H__

#include <db_sqlite3.h>

#define DB_DEVICE_PROFILE_TABLE_NAME            "device_profile"
#define DB_CREATE_DEVICE_PROFILE_TABLE_SQL      "id       INTEGER PRIMARY KEY,"\
                                                "module   TEXT NOT NULL UNIQUE,"\
                                                "protocol TEXT NOT NULL"

#define DB_ATTR_TABLE_NAME                      "attributes_info"
#define DB_CREATE_ATTR_TABLE_SQL                "id         INTEGER PRIMARY KEY,"\
                                                "module     TEXT NOT NULL,"\
                                                "attrid     INTEGER NOT NULL,"\
                                                "permission INTEGER NOT NULL,"\
                                                "channel    INTEGER NOT NULL,"\
                                                "type       INTEGER NOT NULL,"\
                                                "value      TEXT,"\
                                                "UNIQUE (module, attrid, channel)"

#define DB_DYNAMIC_ATTR_TABLE_NAME              "dynamic_attributes"
#define DB_CREATE_DYNAMIC_ATTR_TABLE_SQL        "id         INTEGER PRIMARY KEY,"\
                                                "module     TEXT NOT NULL,"\
                                                "attrid     INTEGER NOT NULL,"\
                                                "permission INTEGER NOT NULL,"\
                                                "channel    INTEGER NOT NULL,"\
                                                "type       INTEGER NOT NULL,"\
                                                "UNIQUE (module, attrid, channel)"

#define DB_DEV_ATTR_MAP_TABLE_NAME              "device_attr_map"
#define DB_CREATE_DEV_ATTR_MAP_TABLE_SQL        "id          INTEGER PRIMARY KEY,"\
                                                "module      TEXT    NOT NULL,"\
                                                "attrs_id    INTEGER NOT NULL,"\
                                                "attrs_type  INTEGER NOT NULL"


#define DB_FIRMWARE_TABLE_NAME                  "firmware_info"
#define DB_CREATE_FIRMWARE_TABLE_SQL            "id          INTEGER PRIMARY KEY,"\
                                                "module      TEXT    NOT NULL UNIQUE,"\
                                                "version     TEXT NOT NULL,"\
                                                "url        TEXT NOT NULL,"\
                                                "md5sum      TEXT NOT NULL"

#define DB_VERSION_TABLE_NAME                   "tables_version"
#define DB_CREATE_VERSION_TABLE_SQL             "name    TEXT NOT NULL UNIQUE,"\
                                                "version TEXT NOT NULL"

#define DB_DEVICES_TABLE_NAME                   "devices"
#define DB_CREATE_DEVICES_TABLE_SQL             "id        INTEGER PRIMARY KEY,"\
                                                "profileid INTEGER,"\
                                                "uuid      TEXT NOT NULL UNIQUE,"\
                                                "wversion  TEXT,"\
                                                "hwversion TEXT,"\
                                                "ethlink   INTEGER DEFAULT 0,"\
                                                "buslink   INTEGER DEFAULT 0,"\
                                                "timestamp INTEGER DEFAULT 0,"\
                                                "ota_state INTEGER DEFAULT 0"


#define DB_UNIDEV_TABLE_NAME    "unidev"

#define DB_UNIDEV_CREATE_UNIDEV_TABLE_SQL       "uuid TEXT PRIMARY KEY,"\
                                                "model TEXT"

#define DB_WHITELIST_TABLE_NAME                 "whitelist"
#define DB_CREATE_WHITELIST_TABLE_SQL           "id        INTEGER PRIMARY KEY,"\
                                                "uuid      TEXT NOT NULL UNIQUE"


typedef int (*dbcallback)(char **, int, int, char **);

extern int db_init(sqlite3db_ts *mdb);

extern int db_deinit(sqlite3db_ts *mdb);

extern int db_save_immediately(sqlite3db_ts *mdb);

#endif